﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Text.RegularExpressions;
using System.Xml.Serialization;
using System.IO;

namespace XEvents.Library
{
    internal class Utilities
    {
        /// <summary>
        /// Regex for Guid Validation
        /// </summary>
        private static readonly Regex isGuid = new Regex(@"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$", RegexOptions.Compiled);

        /// <summary>
        /// Test if a String is a valid Guid and convert if it is.
        /// </summary>
        /// <param name="candidate">String to be checked</param>
        /// <param name="output">Guid from conversion</param>
        /// <returns></returns>
        internal static bool IsGuid(string candidate, out Guid output)
        {
            bool isValid = false;
            output = Guid.Empty;

            if (candidate != null)
            {
                if(isGuid.IsMatch(candidate))
                {
                    output = new Guid(candidate);
                    isValid = true;
                }
            }
            return isValid;
        }

        /// <summary>
        /// return default value for integer if the object is null
        /// </summary>
        /// <param name="i">object to check</param>
        /// <returns>int.MinValue for null or int conversion of the object</returns>
        internal static int IsNull_Int32(object i)
        {
            if (i == null)
                return int.MinValue;
            else
                return (int)i;
        }

        /// <summary>
        /// return default value for decimal if the object is null
        /// </summary>
        /// <param name="o">object to check</param>
        /// <returns>decimal.MinValue for null or decimal conversion of the object</returns>
        internal static decimal IsNull_Decimal(object o)
        {
            if (o == null)
                return decimal.MinValue;
            else
                return (decimal)o;
        }

        /// <summary>
        /// return empty string if the object is null
        /// </summary>
        /// <param name="s">object to be checked</param>
        /// <returns>string.Empty for null or string coversion of the object</returns>
        internal static string IsNull_String(object s)
        {
            if (s == null)
                return string.Empty;
            else
                return (string)s;
        }

        /// <summary>
        /// Trims trailing characters from the script.
        /// </summary>
        /// <param name="script">Script to be trimmed</param>
        internal static string TrimScriptEnd(string script)
        {
            if (script.EndsWith(",\r\n") || script.EndsWith(",") || script.EndsWith(", "))
            {
                script = script.Remove(script.LastIndexOf(','));
            }
            return script;
        }

        internal static System.Xml.XmlDocument GetSerializedXml(System.Type type, object obj)
        {
            System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();
            XmlSerializer serializer = new XmlSerializer(type);
            StringBuilder sb = new StringBuilder();

            using (StringWriter writer = new StringWriter(sb))
            {
                serializer.Serialize(writer, obj);
                xdoc.LoadXml(sb.ToString());
            }
            return xdoc;
        }
    }
}
